#include <string>
#include <json/json.h>
#include <iostream>
#include <fstream>
#include "config.h"
#include <string.h>
#include "common.h"
//#include <stdlib.h>

const char * configFiles_json[MAX_CONFIG_FILE_NUM] = 
{
    "/mnt/nandflash/apps/config/openlid.json",
    "/mnt/nandflash/apps/config/closelid.json",
    "/mnt/nandflash/apps/config/xr.json",
    "/mnt/nandflash/apps/config/bash.json",
    "/mnt/nandflash/apps/config/trans.json",
    "/mnt/nandflash/apps/config/dp8.json",
    "/mnt/nandflash/apps/config/heat_seal.json",
    "/mnt/nandflash/apps/config/dj.json",
    "/mnt/nandflash/apps/config/bath.json",
    "/mnt/nandflash/apps/config/dp8suck.json",
    "/mnt/nandflash/apps/config/dp8tip.json",
    "/mnt/nandflash/apps/config/dp8spit.json",
    "/mnt/nandflash/apps/config/dp8tip.json",
    "/mnt/nandflash/apps/config/xr.json",
    "/mnt/nandflash/apps/config/xr.json",
    "/mnt/nandflash/apps/config/djclean.json",
    "/mnt/nandflash/apps/config/bath.json",    
    "/mnt/nandflash/apps/config/transtube.json",   
    "/mnt/nandflash/apps/config/dp8suck_ab.json",
};


// int getEleCtlIdByString(const char * eleCtlName)
// {
// #ifndef _QPCR
//     if(strcmp(eleCtlName,"openlid_y") == 0)
//     {
//         return 11;
//     }

//     if(strcmp(eleCtlName,"openlid_z") == 0)
//     {
//         return 10;
//     }

//     if(strcmp(eleCtlName,"openlid_jaw") == 0)
//     {
//         return 83;
//     }

//     if(strcmp(eleCtlName,"scancode") == 0)
//     {
//         return 12;
//     }

//     if(strcmp(eleCtlName,"xr_y") == 0)
//     {
//         return 16;
//     }

//     if(strcmp(eleCtlName,"xr_z") == 0)
//     {
//         return 19;
//     }

//     if(strcmp(eleCtlName,"xr_jaw") == 0)
//     {
//         return 18;
//     }

//     if(strcmp(eleCtlName,"xr_r") == 0)
//     {
//         return 17;
//     }

//     if(strcmp(eleCtlName,"dp8_x") == 0)
//     {
//         return 81;
//     }

//     if(strcmp(eleCtlName,"dp8_y") == 0)
//     {
//         return 80;
//     }

//     if(strcmp(eleCtlName,"dp8_z") == 0)
//     {
//         return 15;
//     }

//     if(strcmp(eleCtlName,"dp8_suck") == 0)
//     {
//         return 14;
//     }
// #else

//     if(strcmp(eleCtlName,"bash_zl") == 0)
//     {
//         return 45;
//     }

//     if(strcmp(eleCtlName,"bash_zr") == 0)
//     {
//         return 46;
//     }

//     if(strcmp(eleCtlName,"logistics_y") == 0)
//     {
//         return 44;
//     }

//     if(strcmp(eleCtlName,"logistics_z") == 0)
//     {
//         return 43;
//     }

//     if(strcmp(eleCtlName,"logistics_jaw") == 0)
//     {
//         return 42;
//     }

//     if(strcmp(eleCtlName,"logistics_x") == 0)
//     {
//         return 48;
//     }

//     if(strcmp(eleCtlName,"trans") == 0)
//     {
//         return 41;
//     }
// #endif
//     return 0;
// }

int read_param_json(int fileId,const char* pathInJson,void * paramOut,void * paramIn)
{
    Json::Reader reader;
    Json::Value root;
 
    //从文件中读取，保证当前文件有test.json文件
    std::ifstream in;//(configFiles_json[fileId], std::ios::binary);
    //in.open("test.json", ios::binary);
    //std::cout<<"enter in read_param_json"<<std::endl;
    if(paramIn)
    {
       reader.parse((char*)paramIn,root); 
    }
    else
    {
        in.open(configFiles_json[fileId], std::ios::binary);
        if( !in.is_open() )  
        { 
            std::cout << "Error opening file\n"; 
            return -1; 
        }
        reader.parse(in,root); 

    }
    
    if(root.empty())
    {
        std::cout << "Error parse json content\n"; 
        return -2;
    }
    
    std::string strPath(pathInJson);

    std::vector<std::string> strvec;
    strvec.clear();

    std::string::size_type pos1, pos2;
    pos2 = strPath.find('.');
    pos1 = 0;
    while (std::string::npos != pos2)
    {
        strvec.push_back(strPath.substr(pos1, pos2 - pos1));

        pos1 = pos2 + 1;
        pos2 = strPath.find('.', pos1);
    }
    strvec.push_back(strPath.substr(pos1));
    Json::Value valueTar ;

    //printf("in read_param_json strvec.size:%d\n",strvec.size());
    for(int istr = 0;istr<strvec.size();istr++)
    {
        if(istr==0)
        {
           valueTar = root[strvec[istr]];
        }
        else
        {
           valueTar =  valueTar[strvec[istr]];
        }
        //std::cout<<"keystr:"<<strvec[istr]<<std::endl;
        
    }
    int count = 0;
    
    //std::cout<<"valueTar end"<<std::endl;
    // if(valueTar.isNull())
    // {
    //     std::cout<<"valueTar is empty"<<std::endl;

    // }
    // else
    // {
    //     std::cout<<"valueTar  get num"<<std::endl;
    //     count = valueTar.size();
    //     std::cout<<"num is "<<count<<std::endl;
    // }
    
    //char * memBuff = new char[1024];


    paramProcNode * ptrProcParam = (paramProcNode *)paramOut;
    int * tempPtr = (int*)paramOut;
    paramEleCtl * ptrParamEleCtl = (paramEleCtl *) (tempPtr+1);

    //Json::Value gotoOpen = root["openlid"]["goto_open"]["electl"];
    if(valueTar.isObject() && !valueTar.isArray())
    {
        Json::Value eleCtl = valueTar;
        *tempPtr = 1;
        if(!eleCtl["pre_wait"].empty())
        {
            ptrParamEleCtl->preWait = eleCtl["pre_wait"].asInt(); 
        }
        ptrParamEleCtl->sid = getEleCtlIdByString((char*) eleCtl["ele"].asString().c_str());       
        ptrParamEleCtl->position = eleCtl["position"].asInt();
        ptrParamEleCtl->speed = eleCtl["speed"].asInt();
        ptrParamEleCtl->acc = eleCtl["acc"].asInt();
        ptrParamEleCtl->dec = eleCtl["dec"].asInt(); 

        *tempPtr = 1;
    }
    else if(valueTar.isArray())
    {
        count = valueTar.size();
        *tempPtr = count;
        for(int i=0;i<count;i++)
        {
            Json::Value eleCtl = valueTar[i];
        
            // std::cout<<"ele name:"<<eleCtl["ele"].asString();
            // std::cout<<",position:"<<eleCtl["position"].asString();
            // std::cout<<",speed:"<<eleCtl["speed"].asString();
            // std::cout<<",acc:"<<eleCtl["acc"].asString();
            // std::cout<<",dec:"<<eleCtl["dec"].asString()<<std::endl;
            if(!eleCtl["pre_wait"].empty())
            {
                ptrParamEleCtl->preWait = eleCtl["pre_wait"].asInt(); 
            }
            ptrParamEleCtl->sid = getEleCtlIdByString((char*) eleCtl["ele"].asString().c_str());       
            ptrParamEleCtl->position = eleCtl["position"].asInt();
            ptrParamEleCtl->speed = eleCtl["speed"].asInt();
            ptrParamEleCtl->acc = eleCtl["acc"].asInt();
            ptrParamEleCtl->dec = eleCtl["dec"].asInt();

            ptrParamEleCtl++;
        }

    }   

    root.clear();

    if(paramIn==NULL)
    {
        in.close();
    }

    return 0;    
}

int read_param_json_djpump(int fileId,const char* pathInJson,void * paramOut,void * paramIn)
{
    Json::Reader reader;
    Json::Value root;
 
    //从文件中读取，保证当前文件有test.json文件
    std::ifstream in;//(configFiles_json[fileId], std::ios::binary);
    //in.open("test.json", ios::binary);
    //std::cout<<"enter in read_param_json"<<std::endl;
    if(paramIn)
    {
       reader.parse((char*)paramIn,root); 
    }
    else
    {
        in.open(configFiles_json[fileId], std::ios::binary);
        if( !in.is_open() )  
        { 
            std::cout << "Error opening file\n"; 
            return -1; 
        }
        reader.parse(in,root); 

    }
    
    if(root.empty())
    {
        std::cout << "Error parse json content\n"; 
        return -2;
    }
    
    std::string strPath(pathInJson);

    std::vector<std::string> strvec;
    strvec.clear();

    std::string::size_type pos1, pos2;
    pos2 = strPath.find('.');
    pos1 = 0;
    while (std::string::npos != pos2)
    {
        strvec.push_back(strPath.substr(pos1, pos2 - pos1));

        pos1 = pos2 + 1;
        pos2 = strPath.find('.', pos1);
    }
    strvec.push_back(strPath.substr(pos1));
    Json::Value valueTar ;

    //printf("in read_param_json strvec.size:%d\n",strvec.size());
    for(int istr = 0;istr<strvec.size();istr++)
    {
        if(istr==0)
        {
           valueTar = root[strvec[istr]];
        }
        else
        {
           valueTar =  valueTar[strvec[istr]];
        }
        //std::cout<<"keystr:"<<strvec[istr]<<std::endl;
        
    }
    int count = 0;

    paramProcNode * ptrProcParam = (paramProcNode *)paramOut;
    int * tempPtr = (int*)paramOut;
    paramEleCtl * ptrParamEleCtl = (paramEleCtl *) (tempPtr+1);

    //Json::Value gotoOpen = root["openlid"]["goto_open"]["electl"];
    if(valueTar.isObject() && !valueTar.isArray())
    {
        Json::Value eleCtl = valueTar;
        *tempPtr = 1;
        if(!eleCtl["pre_wait"].empty())
        {
            ptrParamEleCtl->preWait = eleCtl["pre_wait"].asInt(); 
        }
        ptrParamEleCtl->sid = getEleCtlIdByString((char*) eleCtl["ele"].asString().c_str());       
        ptrParamEleCtl->position = eleCtl["openorclose"].asInt(); 

        *tempPtr = 1;
    }
    else if(valueTar.isArray())
    {
        count = valueTar.size();
        *tempPtr = count;
        for(int i=0;i<count;i++)
        {
            Json::Value eleCtl = valueTar[i];
    
            if(!eleCtl["pre_wait"].empty())
            {
                ptrParamEleCtl->preWait = eleCtl["pre_wait"].asInt(); 
            }
            ptrParamEleCtl->sid = getEleCtlIdByString((char*) eleCtl["ele"].asString().c_str());       
            ptrParamEleCtl->position = eleCtl["openorclose"].asInt();

            ptrParamEleCtl++;
        }

    }   

    root.clear();

    if(paramIn==NULL)
    {
        in.close();
    }

    return 0;    
}

// int main(int argc, char *argv[])
// {
//     char * memBuff = new char[1024];
//     memset(memBuff,0,1024);
//     read_param_json(I_FILE_OPENLID,"openlid.goto_open.electl",memBuff,NULL);

//     int * ptrNum =  (int *)memBuff;

//     printf("num of param:%d\n",*ptrNum);
    
//     int num = * ptrNum;

//     paramEleCtl * ptrParamEleCtl = (paramEleCtl *)(ptrNum+1);

//     for(int i=0;i<num;i++)
//     {
//         printf("\n-------\nsid:%d\nposition:%d\nspeed:%d\nacc:%d\ndec:%d\n",
//         ptrParamEleCtl->sid,
//         ptrParamEleCtl->position,
//         ptrParamEleCtl->speed,
//         ptrParamEleCtl->acc,
//         ptrParamEleCtl->dec);

//         ptrParamEleCtl++;
//     }

//     delete [] memBuff;

//     return 0;
// }


void xr_gotoStartY2_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_XR,"/xr/gotoStartY2/electl",dataOut);
#else
    read_param_json(I_FILE_XR,"xr.gotoStartY2.electl",dataOut,NULL);
#endif 
}
void xr_gotoStartY_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_XR,"/xr/gotoStartY/electl",dataOut);
#else
    read_param_json(I_FILE_XR,"xr.gotoStartY.electl",dataOut,NULL);
#endif 
}
void xr_startRotate_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_XR,"/xr/startRotate/electl",dataOut);
#else
    read_param_json(I_FILE_XR,"xr.startRotate.electl",dataOut,NULL);
#endif   
}
void xr_startDown_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_XR,"/xr/startDown/electl",dataOut);
#else
    read_param_json(I_FILE_XR,"xr.startDown.electl",dataOut,NULL);
#endif    
}
void xr_jawclose_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_XR,"/xr/jawclose/electl",dataOut);
#else
    read_param_json(I_FILE_XR,"xr.jawclose.electl",dataOut,NULL);
#endif    
}
void xr_rotate_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_XR,"/xr/rotate/electl",dataOut);
#else
    read_param_json(I_FILE_XR,"xr.rotate.electl",dataOut,NULL);
#endif    
}
void xr_gotoTarY_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_XR,"/xr/gotoTarY/electl",dataOut);
#else
    read_param_json(I_FILE_XR,"xr.gotoTarY.electl",dataOut,NULL);
#endif 
}
void xr_gotoTarY2_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_XR,"/xr/gotoTarY2/electl",dataOut);
#else
    read_param_json(I_FILE_XR,"xr.gotoTarY2.electl",dataOut,NULL);
#endif   
}
void xr_downTar_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_XR,"/xr/downTar/electl",dataOut);
#else
    read_param_json(I_FILE_XR,"xr.downTar.electl",dataOut,NULL);
#endif    
}
void xr_jawopen_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_XR,"/xr/jawopen/electl",dataOut);
#else
    read_param_json(I_FILE_XR,"xr.jawopen.electl",dataOut,NULL);
#endif    
}
void xr_goreset_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_XR,"/xr/goreset1/electl",dataOut);
#else
    read_param_json(I_FILE_XR,"xr.goreset1.electl",dataOut,NULL);
#endif   
}

void xr_goreset2_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_XR,"/xr/goreset2/electl",dataOut);
#else
    read_param_json(I_FILE_XR,"xr.goreset2.electl",dataOut,NULL);
#endif   
}
void heatSeal_gotosafe_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_THREMAL_SEAL,"/heat_seal/gotosafe/electl",dataOut);
#else
    read_param_json(I_FILE_THREMAL_SEAL,"heat_seal.gotosafe.electl",dataOut,NULL);
#endif
}
void heatSeal_xrRotate_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_THREMAL_SEAL,"/heat_seal/xr_rotate/electl",dataOut);
#else
    read_param_json(I_FILE_THREMAL_SEAL,"heat_seal.xr_rotate.electl",dataOut,NULL);
#endif    
}
void heatSeal_gotoheat_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_THREMAL_SEAL,"/heat_seal/gotoheat/electl",dataOut);
#else
    read_param_json(I_FILE_THREMAL_SEAL,"heat_seal.gotoheat.electl",dataOut,NULL);
#endif
}

void heatSeal_heatdown_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_THREMAL_SEAL,"/heat_seal/heatdown/electl",dataOut);
#else
    read_param_json(I_FILE_THREMAL_SEAL,"heat_seal.heatdown.electl",dataOut,NULL);
#endif    
}

void heatSeal_heatup_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_THREMAL_SEAL,"/heat_seal/heatup/electl",dataOut);
#else
    read_param_json(I_FILE_THREMAL_SEAL,"heat_seal.heatup.electl",dataOut,NULL);
#endif   
}
void heatSeal_goreset_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_THREMAL_SEAL,"/heat_seal/goreset/electl",dataOut);
#else
    read_param_json(I_FILE_THREMAL_SEAL,"heat_seal.goreset.electl",dataOut,NULL);
#endif     
}
void heatSeal_rotate_reset_read_param(void * dataOut,void * dataIn)
{
#if _USE_XML_
    read_param_xml(I_FILE_THREMAL_SEAL,"/heat_seal/rotate_reset/electl",dataOut);
#else
    read_param_json(I_FILE_THREMAL_SEAL,"heat_seal.rotate_reset.electl",dataOut,NULL);
#endif  
}

void djclean_read_param(void * dataOut,void * dataIn)
{
    djcleanParamCtl * ptrCtl = (djcleanParamCtl *)dataIn;
    char strPath[128];
    memset(strPath,0,128);    
    
#if _USE_XML_
    sprintf(strPath,"%s%d%s","/dj/step",ptrCtl->stepNum,"/electl");
    read_param_xml(I_MODULE_DJLIQUID,strPath,dataOut);
#else
    sprintf(strPath,"%s%d%s","dj.step",ptrCtl->stepNum,".electl");
    if(ptrCtl->eleType == 0)
    {
        read_param_json(I_MODULE_DJLIQUID,strPath,dataOut,NULL);
    }
    else if(ptrCtl->eleType == 1)
    {
        read_param_json_djpump(I_MODULE_DJLIQUID,strPath,dataOut,NULL);
    }    
#endif   
}






